<template>
    <div style="padding: 10px;">

        <!--功能区域-->
        <div style="margin: 10px 0">

        </div>

        <!--搜索-->
        <div style="margin: 10px; text-align: center;margin-left: -120px;border-radius: 20px">
            <el-input @keyup.enter.native="load" v-model="search" placeholder="请输入关键字"
                      style="border-radius: 20px;width: 20%;" clearable=""></el-input>
            <el-button type="primary" style="margin-left: 7px" @click="load">查询</el-button>
        </div>

        <el-table
                :data="tableData"
                border
                stripe=""
                size="medium"
                style="width: 100%">
            <el-table-column
                    prop="id"
                    sortable=""
                    label="ID"
                    width="100px"
            >
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="用户名">
            </el-table-column>
            <el-table-column
                    prop="nick"
                    label="昵称">
            </el-table-column>

            <el-table-column label="角色列表">

                <template slot-scope="scope">
                    <el-select clearable v-model="scope.row.roles" multiple placeholder="请选择" style="width: 80%">
                        <el-option v-for="item in roles" :key="item.id" :label="item.comment"
                                   :value="item.id"></el-option>
                    </el-select>

                </template>
            </el-table-column>

            <el-table-column
                    width="170px"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="handleChnage(scope.row)">保存角色</el-button>
                </template>
            </el-table-column>
        </el-table>


        <!--分页-->
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>


    </div>

</template>

<script>
    // @ is an alias to /src

    import request from '../utils/request'


    export default {
        name: 'User',

        data() {
            return {
                bookList: [],
                bookVis: false,

                form: {},
                user: {},
                //新增
                roles: [],
                pageNum: 1,
                pageSize: 5,
                username: "",
                nickname: "",
                search: '',
                //当前页
                currentPage: 1,
                total: 0,
                roleName: "",
                tableData: []
            }


        },
        created() {
            let userStr = sessionStorage.getItem("user") || "{}"
            this.user = JSON.parse(userStr)
            this.load()

        },
        methods: {
            //保存权限信息
            handleChnage(row) {
                request.put("/person/changeRole", row).then(res => {
                    if (res.code === 20000) {
                        this.$toast({type: 'success', message: '更新成功!'})

                        //当前登录的用户角色id如果等于当前操作行角色的id，那么就需要重新登录
                        if (res.data === true) {
                            this.$router.push('/login')
                        }

                    }
                })

            },

            //查询
            load() {
                request.get("/person/selectBook", {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        username: this.username,
                        search: this.search
                    }
                }).then(res => {
                    console.log(res)
                    this.tableData = res.data.records
                    this.total = res.data.total
                })

                request.get("/role/all").then(res => {
                    console.log(res)
                    this.roles = res.data
                })
            },


            //页码 每页的个数
            handleSizeChange(pageSize) {
                this.pageSize = pageSize
                this.load()
            },
            //改变当前页
            handleCurrentChange(pageNum) {
                this.currentPage = pageNum
                this.load()
            },

        }
    }
</script>

<style scoped>

    el-input {
        border-radius: 20px
    }
</style>
